package com.thf.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.thf.model.NewsModels;
import com.thf.model.ResultData;
import com.thf.service.NewsService;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class NewsController {

    @Resource
    private NewsService newsService;
    @RequestMapping("/news")
    public ResultData<Page<NewsModels>> findAndPage(@Param("pageNo") Integer pageNo , @Param("pageSize") Integer pageSize) throws ParseException {
        Page<NewsModels> page = newsService.page(new Page<>(pageNo, pageSize));
        List<NewsModels> records = new ArrayList<>();
        for (NewsModels newsModels:page.getRecords()) {
            String createTime = newsModels.getCreateTime();
            Date parse = new SimpleDateFormat("yyyy-MM-dd").parse(createTime);
            String format = new SimpleDateFormat("yyyy-MM-dd").format(parse);
            newsModels.setCreateTime(format);
            records.add(newsModels);
        }
        page.setRecords(records);
        //反馈给前端--返回值
        return new ResultData<>(0, page);
    }
}
